package com.xhwl.centiotdata.handler.device.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.dahua.DeviceDaHuaDTO;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.virtualDevice.VirtualDeviceHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.pedes.DeviceAttributeDTO;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.pojo.cent.device.Device;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DeviceVirtualDevicesHandler extends DeviceIntegrationHandler {
    private static final Logger log = LoggerFactory.getLogger(DeviceVirtualDevicesHandler.class);

    @Autowired
    private VirtualDeviceHandler virtualDeviceHandler;

    @Autowired
    private IDeviceCentService deviceCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        DeviceIntegrationFactory.register(62, this);
    }

    @Override
    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDevice failed! deviceType is null!", configDTO.getProjectId());
            return;
        }
        // 拉取数据
        List<DeviceDaHuaDTO> deviceDaHuaDTOList = virtualDeviceHandler.pullDevice(configDTO);
        if (CollectionUtils.isEmpty(deviceDaHuaDTOList)) {
            return;
        }
        // 转换数据
        List<DeviceDTO> deviceDTOList = this.handleDevice(configDTO, deviceDaHuaDTOList);
        // 存储业务中台
        deviceCentService.asyncUpdateDevice(deviceDTOList);
    }

    @Override
    public void pullDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDeviceOnlineStatus failed! deviceType is null!", configDTO.getProjectId());
            return;
        }
        // 拉取数据
        List<DeviceDaHuaDTO> deviceDaHuaDTOList = virtualDeviceHandler.pullDevice(configDTO);
        if (CollectionUtils.isEmpty(deviceDaHuaDTOList)) {
            return;
        }
        // 转换数据
        List<Device> deviceList = this.handleDeviceOnlineStatus(configDTO, deviceDaHuaDTOList);
        // 存储业务中台
        deviceCentService.asyncUpdateOnlineStatus(deviceList);
    }

    private List<DeviceDTO> handleDevice(DeviceIntegrationConfigDTO configDTO, List<DeviceDaHuaDTO> deviceDaHuaDTOList) {
        return deviceDaHuaDTOList.stream().map(deviceDaHuaDTO -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setCategoryId(configDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
            deviceDTO.setName(deviceDaHuaDTO.getChannelName());
            deviceDTO.setThirdDeviceId(deviceDaHuaDTO.getChannelId());
            deviceDTO.setOnlineStatus(deviceDaHuaDTO.getIsDeviceOnline() ? DeviceOnlineStatusEnum.ONLINE.id : DeviceOnlineStatusEnum.OFFLINE.id);
            JSONObject attributeJson = null;
            if (configDTO.getDeviceType().intValue() == 1) {
                DeviceAttributeDTO deviceAttributeDTO = new DeviceAttributeDTO();
                deviceAttributeDTO.setIp(deviceDaHuaDTO.getDeviceIp());
                attributeJson = (JSONObject) JSONObject.toJSON(deviceAttributeDTO);
            }
            deviceDTO.setAttributeJson(attributeJson);
            return deviceDTO;
        }).collect(Collectors.toList());
    }

    private List<Device> handleDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO, List<DeviceDaHuaDTO> deviceDaHuaDTOList) {
        return deviceDaHuaDTOList.stream().map(deviceDaHuaDTO -> {
            Device device = new Device();
            device.setProjectId(configDTO.getProjectId());
            device.setThirdDeviceId(deviceDaHuaDTO.getChannelId());
            device.setDeviceTypeId(configDTO.getDeviceType());
            device.setOnlineStatus(deviceDaHuaDTO.getIsDeviceOnline() ? DeviceOnlineStatusEnum.ONLINE : DeviceOnlineStatusEnum.OFFLINE);
            return device;
        }).collect(Collectors.toList());
    }
}
